package com.zzy.caching.demo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import org.junit.Test;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;

public class CacheTest {

	public static Map<String,List<String>>  datamap=new HashMap<String,List<String>>();
	static{
		CacheTest.datamap.put("123", Lists.newArrayList("123","456"));
		CacheTest.datamap.put("456", Lists.newArrayList("789","9","8"));
		datamap=ImmutableMap.copyOf(datamap);
	}
        //缓存接口这里是LoadingCache，LoadingCache在缓存项不存在时可以自动加载缓存
    public static LoadingCache<String,List<String>> studentCache
                //CacheBuilder的构造函数是私有的，只能通过其静态方法newBuilder()来获得CacheBuilder的实例
                = CacheBuilder.newBuilder()
                //设置并发级别为8，并发级别是指可以同时写缓存的线程数
                .concurrencyLevel(8)
                //设置写缓存后8秒钟过期
                .expireAfterWrite(8, TimeUnit.SECONDS)
                //设置缓存容器的初始容量为10
                .initialCapacity(10)
                //设置缓存最大容量为100，超过100之后就会按照LRU最近虽少使用算法来移除缓存项
                .maximumSize(100)
                //设置要统计缓存的命中率
                .recordStats()
                //设置缓存的移除通知
                .removalListener(new RemovalListener<Object, Object>() {
                    public void onRemoval(RemovalNotification<Object, Object> notification) {
                        System.out.println(notification.getKey() + " was removed, cause is " + notification.getCause());
                    }
                })
                //build方法中可以指定CacheLoader，在缓存不存在时通过CacheLoader的实现自动加载缓存
                .build(
                        new CacheLoader<String, List<String>>() {
                            @Override
                            public List<String> load(String key) throws Exception {
                            	System.out.println("load  key:"+key);
                            	List<String> list=new ArrayList<String>();
								return list;
                            }
                        }
                );
    
  @Test  
 public void TestCade() throws ExecutionException, InterruptedException{
	            studentCache.put("123", datamap.get("123"));
	 
        for (int i=0;i<10;i++) {
            //从缓存中得到数据，由于我们没有设置过缓存，所以需要通过CacheLoader加载缓存数据
            List<String> list= studentCache.get("123");
            System.out.println(list.size());
            
            List<String> list2= studentCache.get("456");
            System.out.println(list2.size());
            //休眠1秒
            TimeUnit.SECONDS.sleep(1);
        }

        System.out.println("cache stats:");
        //最后打印缓存的命中率等 情况
        System.out.println(studentCache.stats().toString());
    }

}
